#ifndef __APP_COMMON_H__
#define __APP_COMMON_H__

#ifdef __cplusplus
extern "C"
{
#endif
#include <string.h>
#include "tkl_uart.h"
#include "tal_sw_timer.h"
#include "tuya_cloud_types.h"
#include "tuya_zigbee_stack.h"

/**************************************************************************
 *                              global  variable
 * ************************************************************************/
#define USER_KEY0           TUYA_GPIO_NUM_19
#define USER_PWM_CH0_PIN    TUYA_GPIO_NUM_12
#define USER_PWM_CH1_PIN    TUYA_GPIO_NUM_13
#define USER_UART0          TUYA_UART_NUM_0


#define KV_ID_RST_CNT 	    (0u)
#define KV_ID_WARM_ST 	    (1u)
#define KV_ID_APP_DATA 	    (2u)

#define ZCL_ID_ONOFF            100
#define ZCL_ID_MODE             101
#define ZCL_ID_BRIGHT           102
#define ZCL_ID_TEMPER           103
#define ZCL_ID_COUNT_DOWN       104
#define ZCL_ID_TY_SCENE         105
#define ZCL_ID_RHYTHM_MODE      106
#define ZCL_ID_POWER_MEMORY     107
#define ZCL_ID_DO_NOT_DISTURB   108
#define ZCL_ID_SWITCH_GRADIENT  109
#define ZCL_ID_PRIVATE_TUYA     110

#define PWM_DUTY_500        500
#define TWO_PWM_CHANNELS    2
#define PWM_FREQUENCY_1000  1000

#define TEMP_6500K 153 ///* temp =1000000/attr   500 = 1000000/2000
#define TEMP_2000K 500

#define APP_WHITE_B_MAX     1000
#define APP_WHITE_T_MAX     1000
#define APP_COLOR_H_MAX     360
#define APP_COLOR_S_MAX     1000
#define APP_COLOR_V_MAX     1000

#define ZIGBEE_WHITE_B_MAX  255 ///< color: level
#define ZIGBEE_WHITE_T_MAX  255 ///< color: temperature
#define ZIGBEE_COLOR_H_MAX  254 ///< color: Hue
#define ZIGBEE_COLOR_S_MAX  254 ///< color: Saturation
#define ZIGBEE_COLOR_V_MAX  255 ///< color: value

#define ZIGBEE_JOIN_MAX_TIMEOUT         (180*1000UL) //3min

#define OPTION_ATTRIBUTE_ID             0x000F

///< on_off cluster
//cmd
#define TY_CMD_SET_COUNT_DOWN_CMD_ID            0xF0 ///<   tuya private cmd
//attr id
#define TY_ATTR_COUNT_DOWN_ATTR_ID              0xF000

///< level cluster
//cmd
#define TY_CMD_SET_BRIGHT_CMD_ID                0xF0    ///< cmd: bright
//attr id
#define TY_ATTR_CURRENT_BRIGHT_ATTR_ID          0xF000


///< color cluster
//cmd
#define TY_CMD_SET_TEMPERATURE_CMD_ID           0xE0    ///< cmd: temperature
#define TY_CMD_SET_LIGHT_MODE_CMD_ID            0xF0    ///< change mode cmd: white/color/scene/music
#define TY_CMD_SET_TY_SCENE_CMD_ID              0xF1
#define TY_CMD_SET_RHYTHM_MDOE_CMD_ID           0xF6
#define TY_CMD_SET_POWER_MEMORY_CMD_ID          0xF9
#define TY_CMD_SET_DND_CMD_ID                   0xFA
#define TY_CMD_SET_SWITCH_GRADIENT_CMD_ID       0xFB
//attr id
#define TY_ATTR_TEMPERATURE_ATTR_ID             0xE000  ///<
#define TY_ATTR_LIGHT_MODE_ATTRIBUTE_ID         0xF000  ///< light mode attribute (1 byte)
#define TY_ATTR_LIGHT_ALL_DATA_ATTRIBUTE_ID     0xF102  ///< all app data attribute: mode(1) + H(1) + S(1) + V(1) + B(1) + T(2) = 7 Bytes
#define TY_ATTR_TY_SCENE_ATTR_ID                0xF003  ///<
#define TY_ATTR_COLOR_RHYTHM_MDOE_ATTR_ID       0xF009
#define TY_ATTR_POWER_MEMORY_ATTR_ID            0xF00C
#define TY_ATTR_DND_ATTR_ID                     0xF00D
#define TY_ATTR_SWITCH_GRADIENT_ATTR_ID         0xF00E

///< PRIVATE TUYA
//cmd
#define TY_CMD_SET_PRIVATE_TUYA_CMD_ID          0x04

//生物节律数据
// rhythm_mode head(scene Num) length
#define RHYTHM_MODE_HRAD_LENGTH         5
// rhythm_mode unit length
#define RHYTHM_MODE_UNIT_LENGTH         9
// rhythm_mode max unit number
#define RHYTHM_MODE_MAX_UNIT            6
// rhythm_mode data min length, formu -> 5 + 9 = 14
#define RHYTHM_MODE_MIN_LENGTH          (RHYTHM_MODE_HRAD_LENGTH + RHYTHM_MODE_UNIT_LENGTH)
// rhythm_mode data max length, formu -> 5 + 9 * 6  = 59
#define RHYTHM_MODE_MAX_LENGTH          (RHYTHM_MODE_HRAD_LENGTH + RHYTHM_MODE_UNIT_LENGTH * RHYTHM_MODE_MAX_UNIT)

//断电记忆
#define POWER_MEMORY_DATA_LENGTH        12

//开关渐变
#define SWITCH_GRADIENT_DATA_LENGTH     7

//涂鸦场景数据
// ty_scene head(scene Num) length
#define TY_SCENE_HRAD_LENGTH            2
// ty_scene unit length
#define TY_SCENEE_UNIT_LENGTH           8
// ty_scene max unit number
#define TY_SCENE_MAX_UNIT               8
// ty_scene data min length, formu -> 2 + 8 = 10
#define TY_SCENE_MIN_LENGTH          (TY_SCENE_HRAD_LENGTH + TY_SCENEE_UNIT_LENGTH)
// ty_scene data max length, formu -> 2 + 8 * 8  = 66
#define TY_SCENE_MAX_LENGTH          (TY_SCENE_HRAD_LENGTH + TY_SCENEE_UNIT_LENGTH * TY_SCENE_MAX_UNIT)

#define APP_DATA_FALSH_HEAD 0x11111111

#ifndef OPRT_OK
#define OPRT_OK 0
#endif
#ifndef OPRT_INVALID_PARM
#define OPRT_INVALID_PARM 1
#endif
#ifndef OPRT_COM_ERROR
#define OPRT_COM_ERROR 2
#endif

#define PIN_NOEXIST 0xFF ///< default pin def

typedef struct
{
    UCHAR_T is_warm_start;
    UCHAR_T rst_cnt;
    UCHAR_T padding[2];
} RST_INFO_T;
typedef struct
{
    USHORT_T H;
    USHORT_T S;
    USHORT_T V;
} APP_HSV_T;
typedef struct
{
    UCHAR_T h;
    UCHAR_T s;
    UCHAR_T v;
} ZIGBEE_HSV_T;
typedef struct
{
    UINT32_T head;
    UINT8_T onoff;
    UINT8_T mode;
	UINT16_T bright;
	UINT16_T temper;
    UINT8_T do_not_disturb;
    UINT8_T rhythm_mode[RHYTHM_MODE_MAX_LENGTH + 1];
    UINT8_T power_memory[POWER_MEMORY_DATA_LENGTH + 1];
    UINT8_T switch_gradient[SWITCH_GRADIENT_DATA_LENGTH + 1];
    UINT8_T ty_scene[TY_SCENE_MAX_LENGTH + 1];
} APP_DATA_T;
typedef struct
{
    UCHAR_T onoff;
    UCHAR_T mode;
    UINT16_T H;
    UINT16_T S;
    UINT16_T V;
    UINT16_T B;
    UINT16_T T;
} APP_ALL_DATA_T;

typedef enum
{
    ZIGBEE_CMD_SINGLE = 0,
    ZIGBEE_CMD_GROUP
} ZIGBEE_CMD_T;

/**************************************************************************
 *                              init functions
 * ************************************************************************/
/**
 * @berief: light init
 * @param [in] none 
 * @return: OPERATE_RET
 */
OPERATE_RET app_light_init(VOID_T);
VOID_T app_button_init(VOID_T);
OPERATE_RET app_pwm_init(VOID_T);
VOID_T dev_uart_output(IN CONST CHAR_T *str);
VOID_T app_button_scan_cb(TIMER_ID timer_id, VOID_T *arg);

/**************************************************************************
 *                          flash functions
 * ************************************************************************/

UCHAR_T app_zcl_get_option_value(ZG_CLUSTER_ID_E cluster, UCHAR_T optionMask, UCHAR_T optionOverride);

#ifdef __cplusplus
}
#endif

#endif
